package fenetres;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Vector;

import javax.imageio.ImageIO;
import javax.swing.*;

import modeles.*;

import controles.*;

public class AddProdFrame extends JInternalFrame implements ActionListener{
	
	Utilisateur userConnected;
	JPanel fondPanel,entetePanel;
	public JTextField codeProdTxt,libProdTxt,priceTxt;
	public JComboBox categCombo;
	public JButton addProdBut,cancelProdBut;
	
	public Vector<Categorie> listCateg;
	
	public AddProdFrame(Utilisateur _userConnected){
		super("Ajout Produit",
	              false, //resizable
	              true, //closable
	              false, //maximizable
	              false);//iconifiable
		
		userConnected = _userConnected;
		boolean accept = checkUserConnected();
		if(accept){
			fondPanel = new JPanel();
			fondPanel.setLayout(null);
			
			etablishEntete();
			etablishFrame();
			this.getContentPane().setLayout(null);
			this.getContentPane().add(entetePanel);
			this.getContentPane().add(fondPanel);
			setLocation(30, 100);
			this.setSize(700,450);
			this.setTitle("Ajout produit");
			this.setVisible(true);
			this.setResizable(false);
		}
	}
	
	public boolean checkUserConnected(){
		//userConnected = accueil.authent.getUserConnected();
		if( userConnected == null){
			JOptionPane.showMessageDialog(null, "Aucun utilisateur connecte!! \n Veuillez connecter svp :) ","Erreur",JOptionPane.ERROR_MESSAGE);
			this.dispose();
			return false;
		}
		else {
			return true;
		}
	}

	public void etablishEntete(){
		entetePanel = new JPanel();
		entetePanel.setBounds(0,0,700,80);
		entetePanel.setBackground(Color.white);
		
		JLabel enteteLab  = new JLabel ("Ajout d'un nouveau Menu");
		enteteLab.setFont(new java.awt.Font("Century Schoolbook L", java.awt.Font.BOLD, 20));
		enteteLab.setForeground(new Color(12,59,234));
		enteteLab.setBounds(200,30,300,20);
		
		entetePanel.setLayout(null);
		entetePanel.add(enteteLab);
	}
	//elaboration des objets sur la fenetre
	public void etablishFrame(){
		fondPanel = new JPanel();
		fondPanel.setBounds(0,80,700,370);
		fondPanel.setLayout(null);
		
		//image
		JLabel imageLab = null;
		try {
			BufferedImage picture = ImageIO.read(new File("images/prod1.png"));
			imageLab = new JLabel (new ImageIcon(picture));
		
		} catch (IOException e) {
			e.printStackTrace();
		}
		imageLab.setBounds(20,40,300,200);
		fondPanel.add(imageLab);
		
		//information sur le produit
		//chargemenr des categories dans la base de donnees
		DBManager dbManager = new DBManager();
		Produit oProd = new Produit ();
		
		 listCateg = oProd.getListCategory(dbManager);
		String[] categorieTab = new String [listCateg.size()];
		for(int i=0; i<listCateg.size() ; i++){
			Categorie categorie = listCateg.get(i);
			categorieTab[i] = categorie.getLibelle();
		}
		
		//recuperer le code du produit suivant
		String codeProd = etablishNextCodeProd();
		
		JLabel codeProdLab = new JLabel("Code: ");
		codeProdTxt = new JTextField();
		codeProdTxt.setText(codeProd);
		codeProdTxt.setEditable(false);
		JLabel libProdLab = new JLabel("Libelle: ");
		libProdTxt = new JTextField();
		JLabel categLab = new JLabel("Categorie: ");
		categCombo = new JComboBox(categorieTab);
		JLabel priceLab = new JLabel("Prix: ");
		priceTxt = new JTextField();
		JLabel umLab = new JLabel("Dollars");
		
		//bouttons de validation et annulation
		addProdBut = new JButton("Ajouter");
		//les caracteristiques du produits
		
		addProdBut.addActionListener(new AddProductAction (this));//new addProductAction(codeProdTxt.getText(),libProdTxt.getText(),categCombo.getSelectedIndex(),priceTxt.getText(),listCateg));
		cancelProdBut = new JButton("Annuler");
		cancelProdBut.addActionListener(new AddProductAction (this));
		
		fondPanel.add(codeProdLab);
		fondPanel.add(codeProdTxt);
		fondPanel.add(libProdLab);
		fondPanel.add(libProdTxt);
		fondPanel.add(categLab);
		fondPanel.add(categCombo);
		fondPanel.add(priceLab);
		fondPanel.add(priceTxt);
		fondPanel.add(umLab);
		fondPanel.add(addProdBut);
		fondPanel.add(cancelProdBut);
		
		codeProdLab.setBounds(380,10,80,30);
		codeProdTxt.setBounds(460,10,150,30);
		libProdLab.setBounds(380,60,80,30);
		libProdTxt.setBounds(460,60,150,30);
		categLab.setBounds(380,110,80,30);
		categCombo.setBounds(460,110,150,30);
		priceLab.setBounds(380,160,80,30);
		priceTxt.setBounds(460,160,150,30);
		umLab.setBounds(615,160,150,30);
		
		addProdBut.setBounds(380,250,140,30);
		cancelProdBut.setBounds(540,250,140,30);
	}
	
	public String etablishNextCodeProd(){
		String codeProd = "";
		DBManager dbManager = new DBManager();
		Produit oProd = new Produit ();
		int idMaxProd = oProd.getMaxtypeProdId(dbManager);
		if(idMaxProd < 10) codeProd = "P000"+idMaxProd;
		else if(idMaxProd<100) codeProd = "P00"+idMaxProd;
		else if (idMaxProd <1000) codeProd = "P0"+idMaxProd;
		else if(idMaxProd < 10000) codeProd = "P"+idMaxProd;
		return codeProd;
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		
	}
}
